package com.koron.order.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.BaseQueryBean;
import com.koron.common.core.util.CodeTools;
import com.koron.order.common.bean.po.OrderAccept;
import com.koron.order.common.mapper.OrderAcceptMapper;
import com.koron.order.common.service.OrderAcceptService;
import com.koron.order.defect.service.DefectService;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (OrderAccept)表服务实现类
 *
 * @author zhouj
 * @since 2021-09-06 11:35:25
 */
@Service("orderAcceptService")
public class OrderAcceptServiceImpl implements OrderAcceptService {

    @Autowired
    DefectService defectService;

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, OrderAccept orderAccept) {

        orderAccept.setId(CodeTools.getCode32());
        orderAccept.setCreateBy(getCurrUser().getAccount());
        orderAccept.setCreateByName(getCurrUser().getName());
        int i = factory.getMapper(OrderAcceptMapper.class).insert(orderAccept);

        /*if (orderAccept.getIsCommit()) {
            defectService.submitDefect(factory, orderAccept.getOrderId(),DefectStatus.ARCHIVED.getStatus());
        }*/

        return i;
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, OrderAccept orderAccept) {

        orderAccept.setUpdateBy(getCurrUser().getAccount());
        orderAccept.setUpdateByName(getCurrUser().getName());
        int i = factory.getMapper(OrderAcceptMapper.class).update(orderAccept);

        /*if (orderAccept.getIsCommit()) {
            defectService.submitDefect(factory, orderAccept.getOrderId(),DefectStatus.ARCHIVED.getStatus());
        }*/

        return i;
    }

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, OrderAccept orderAccept) {

        int rsp;

        if (StringUtils.isEmpty(orderAccept.getId())) {
            rsp = insert(factory, orderAccept);
        } else {
            rsp = update(factory, orderAccept);
        }

        /*if (orderAccept.getIsCommit()) {
            DefectBean defectBean = new DefectBean();
            defectBean.setId(orderAccept.getOrderId());
            defectBean.setStatus(DefectStatus.WAIT_ACCEPT.getStatus());
            defectService.submitDefect(factory, defectBean);
        }*/

        return rsp;

    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {
        int i = factory.getMapper(OrderAcceptMapper.class).deleteById(id);
        return i;
    }

    @Override
    @TaskAnnotation("queryById")
    public OrderAccept queryById(SessionFactory factory, String id) {

        OrderAccept orderAccept = factory.getMapper(OrderAcceptMapper.class).queryById(id);

        return orderAccept;
    }

    @Override
    @TaskAnnotation("queryByOrderId")
    public OrderAccept queryByOrderId(SessionFactory factory, String orderId) {

        OrderAccept orderAccept = factory.getMapper(OrderAcceptMapper.class).queryByOrderId(orderId);

        return orderAccept;
    }

    @Override
    @TaskAnnotation("queryList")
    public PageInfo queryList(SessionFactory factory, OrderAccept orderAccept, BaseQueryBean pageBean) {

        PageHelper.startPage(pageBean.getPage(), pageBean.getPageSize());

        List<OrderAccept> list = factory.getMapper(OrderAcceptMapper.class).queryList(orderAccept);

        return new PageInfo(list);
    }

}